﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using FileManagerGlobal_Library;
using Microsoft.Win32;

namespace FileManager_UnInstallDelete
{
    public partial class FilemanagerDeleteApp : Form
    {
        public FilemanagerDeleteApp(string applicationPath)
        {
            InitializeComponent();
            if (MessageBox.Show("确定要删除 部门文件柜客户端程序 以及所有客户端程序文件吗?", "卸载提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                var processes = Process.GetProcesses();
                foreach (var process in processes)
                {
                    var processName = process.ProcessName.Replace(".exe","");
                    processName = processName.Replace(".vshost.exe", "");
                    processName = processName.Replace(".vshost", "");
                    if ( !GlobalVariable.UpdateApplicationName.StartsWith(processName) &&
                        !GlobalVariable.ClientAppVariable.ClientApplicationName.StartsWith(processName)) continue;
                    MessageBox.Show("检测到有 部门文件柜客户端程序 相关程序正在运行\r\n请关闭相关程序，或者重启计算机?", "卸载提示", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    process.Kill();
                    Environment.Exit(Environment.ExitCode);
                    break;
                }
                //删除注册表自动启动
                try
                {
                    var hkml = Registry.LocalMachine;
                    var software = hkml.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryAutoStartKey, true);
                    if (null != software)
                        software.DeleteValue(GlobalVariable.ClientAppRegistryInfo.AutoStartNameCollection.RegistryAutoStartName);
                    hkml.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //删除安装目录注册表
                try
                {
                    var hkml = Registry.LocalMachine;
                    var software = hkml.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryInstallKey, true);
                    if (null != software)
                    {
                        var hasKey = software.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryInstallName);
                        if (null != hasKey)
                            software.DeleteSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryInstallName);
                    }
                    hkml.Close();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //删除Uninstall注册表
                try
                {
                    var hkml = Registry.LocalMachine;
                    var software = hkml.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryUnInstallKey, true);
                    if (null != software)
                    {
                        var subKey = software.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryUnInstallName);
                        if (null != subKey)
                        {
                            var localtion =
                                   subKey.GetValue(
                                       GlobalVariable.ClientAppRegistryInfo.UninstallNameCollection.InstallLocation);
                            if (null != localtion)
                            {
                                software.DeleteSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryUnInstallName);
                                //删除目录文件
                                var installPath = localtion.ToString();
                                if (Directory.Exists(installPath))
                                {
                                    Directory.Delete(installPath, true);
                                }
                                    
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(applicationPath))
                                if (Directory.Exists(applicationPath))
                                {
                                    Directory.Delete(applicationPath, true);
                                }
                        }
                    }
                    hkml.Close();
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrEmpty(applicationPath))
                        if (Directory.Exists(applicationPath))
                        {
                            Directory.Delete(applicationPath, true);
                        }
                    MessageBox.Show(ex.Message);
                }
                try
                {
                    var startUpFolderPath =
                  Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    var di = new DirectoryInfo(startUpFolderPath);
                    var files = di.GetFiles("部门文件柜客户端.lnk");
                    foreach (var fi in files)
                    {
                            File.Delete(fi.FullName);
                    }
                }
                catch
                {
                }
                MessageBox.Show("卸载成功!", "卸载提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Environment.Exit(Environment.ExitCode);
            }
            else
            {
                Environment.Exit(Environment.ExitCode);
            }
        }

        #region hide application
        private bool _windowCreate = true;
        protected override void OnActivated(EventArgs e)
        {

            if (_windowCreate)
            {
                base.Visible = false;
                _windowCreate = false;
            }
            base.OnActivated(e);
        }
        protected override CreateParams CreateParams
        {
            get
            {
                var WS_EX_TOOLWINDOW = 0x80;
                var CP = base.CreateParams;
                CP.ExStyle = CP.ExStyle | WS_EX_TOOLWINDOW;
                return CP;
            }
        }
        #endregion
    }
}
